package com.more.offer.javabeans;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyEditorSupport;
import java.util.stream.Stream;

/**
 * @author pinoc
 * @date 2022/11/27
 */
public class BeansInfoDemo {
	public static void main(String[] args) throws IntrospectionException {
		BeanInfo person = Introspector.getBeanInfo(Person.class, Object.class);
		Stream.of(person.getPropertyDescriptors()).forEach(var -> {
			String name = var.getName();
			if("age".equals(name)) {
				var.setPropertyEditorClass(StringToIntegerPropertyEditorClass.class);
			}
		});
	}

	static class StringToIntegerPropertyEditorClass extends PropertyEditorSupport {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			Integer value = Integer.valueOf(text);
			setValue(value);
		}
	}

}
